package io.terminus.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.common.log.annotation.SpzxLog;
import io.terminus.spzx.common.log.enums.OperatorType;
import io.terminus.spzx.manager.service.BrandService;
import io.terminus.spzx.model.entity.product.Brand;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 品牌管理Controller
 * @Date 2024-10-07
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "品牌管理接口")
@RequestMapping(value = "/admin/product/brand")
public class BrandController {

    /**
     * 通过final关键字实现Spring依赖注入
     */
    private final BrandService brandService;

    /**
     * 分页查询品牌列表
     *
     * @param page  当前页
     * @param limit 每页记录数
     * @return 品牌分页列表
     */
    @GetMapping("/{page}/{limit}")
    @SpzxLog(title = "分页查询品牌列表", businessType = 0, operatorType = OperatorType.MANAGE)
    public Result<PageInfo<Brand>> list(@PathVariable Integer page, @PathVariable Integer limit) {
        PageInfo<Brand> pageInfo = brandService.findByPage(page, limit);

        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有品牌
     *
     * @return 品牌列表
     */
    @GetMapping("/findAll")
    public Result<List<Brand>> findAll() {
        List<Brand> list = brandService.findAll();

        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加品牌
     *
     * @param brand 品牌
     * @return 添加结果
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody Brand brand) {
        brandService.save(brand);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改品牌
     *
     * @param brand 品牌
     * @return 修改结果
     */
    @PutMapping("updateById")
    public Result<Void> updateById(@RequestBody Brand brand) {
        brandService.updateById(brand);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除品牌
     *
     * @param id 品牌ID
     * @return 删除结果
     */
    @DeleteMapping("/deleteById/{id}")
    public Result<Void> deleteById(@PathVariable Long id) {
        brandService.deleteById(id);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
